package smcms.service.sso.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import smcms.service.sso.entity.*;
import smcms.service.sso.mapper.AccountMapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserDetailService implements UserDetailsService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
        public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        AccountResp userInfo = new AccountResp();
        AccountReq account = accountMapper.getAccountByUsername(s);
        userInfo.setUsername(s);
        userInfo.setPassword(passwordEncoder.encode(account.getPassword()));
        List<Permissions> permissions = accountMapper.queryPermissionByUsername(s);
        userInfo.setCloudId(account.getCloudId());
        List<String> collect = permissions.stream().map(p -> {
            return p.getServiceName() + ":" + p.getPermissionName();
        }).collect(Collectors.toList());
        userInfo.setPermission(collect);
        userInfo.setStaffName(account.getStaffName());
        return userInfo;
    }
}
